local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script,script.Parent.Base);
local ConnectClass=_G.CommonOHOH.load.Connect();
local InstanceManager = CommonOHOH.load.InstanceManager();
-- 车身
function M.new(...)
	return M(...);
end;

function M.ctor(self,...)--构造函数
	M.super.ctor(self,...);
	
end;

function M.dtor(self)
	M.super.dtor(self);
end;



function M.Assemble(self)

	self.Instance.Name = "Body"
	self.Instance.Parent = self.Car:GetModel()
	
	self.ItemList=self.Instance:GetChildren()
	for k,v in pairs(self.ItemList) do
		v.Anchored = false
		v.CanCollide = false
		self:Weld2Part(v,self.Instance)
	end
	

	self.PointFolder=Instance.new("Folder",self.Instance)
	self.PointFolder.Name="Point"

	self.FlagPoint=self.Model:WaitForChild("Flag")
	self.FlagPoint.Parent=self.PointFolder
	self:Weld2Part(self.FlagPoint,self.Instance)

	self.TailPoint=self.Model:WaitForChild("Tail")
	self.TailPoint.Parent=self.PointFolder
	self:Weld2Part(self.TailPoint,self.Instance)

	self.WeaponPoint=self.Model:WaitForChild("Weapon")
	self.WeaponPoint.Parent=self.PointFolder
	self:Weld2Part(self.WeaponPoint,self.Instance)

	self.Ring=self.Model:WaitForChild("Ring")
	self.Ring.Parent=self.PointFolder

	self.RingList=self.Ring:GetChildren()

	for _,v in pairs(self.RingList) do
		self:Weld2Part(v,self.Ring)
	end
	self:Weld2Part(self.Ring,self.Instance)

	self.Player=self.Model:WaitForChild("Player")
	self.Player.Parent=self.PointFolder
	self.PlayerList=self.Player:GetChildren()

	-- local seat = Instance.new("Seat")
	-- 	seat.CanCollide = false
	-- 	seat.Size = Vector3.new(0.1,0.1,0.1)
	-- 	seat.CFrame = self.Player:GetPivot()
	-- 	self:Weld2Part(seat,self.Instance)
	-- 	seat.Parent = self.Instance
	self.SeatList={}
	for k,v in pairs(self.PlayerList) do
		self:Weld2Part(v,self.Player)

		local seat = Instance.new("Seat", self.Instance)
		seat.CanCollide = false
		seat.Size = Vector3.new(0.1,0.1,0.1)
		seat.CFrame = v:GetPivot()
		self:Weld2Part(seat,self.Instance)
		table.insert(self.SeatList,seat)
	end
	
	--将车身约束到引擎上
	local engine=self.Car:GetEngine()
	local engineBody=engine:CreateBody(self:GetEnginePivot())--创建动力引擎
    local weld = Instance.new("WeldConstraint",self.Instance)
	weld.Part0 = self.Instance
	weld.Part1 = engineBody
	--self.Instance.Anchored=true
end

--获取装饰点位
function M.GetFlagPivot(self)
	return self.FlagPoint:GetPivot()
end

--获取推进器点位
function M.GetTailPivot(self)
	return self.TailPoint:GetPivot()
end

--获取钻头点位
function M.GetWeaponPivot(self)
	return self.WeaponPoint:GetPivot()
end

--获取轮胎点位
function M.GetRingPivot(self)
	local list={}
	for _,v in pairs(self.RingList) do
		table.insert(list,v:GetPivot())
	end
	return list
end

--获取引擎的位置
function M.GetEnginePivot(self)
	return self.Ring:GetPivot()
end



--获取玩家点位
function M.GetPlayerPivot(self)
	local list={}
	for _,v in pairs(self.PlayerList) do
		table.insert(list,v:GetPivot())
	end
	return list
end
function M.GetSeatList(self)
	return self.SeatList
end

function M.AddCharacter(self,character)
	for _,s in pairs(self.SeatList)  do
		if  not s:GetAttribute("isUsing") then

			s.Disabled =true
			s:SetAttribute("isUsing",true)
			-- s:Sit(character:FindFirstChildOfClass("Humanoid"))
			-- print("坐车成功！",s)
			-- return true
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			local humanoidRootPart = character:WaitForChild("HumanoidRootPart")  
			local RootAttachment = humanoidRootPart:WaitForChild("RootAttachment") 
			local RootRigAttachment = humanoidRootPart:WaitForChild("RootRigAttachment") 
			
			local seatDelt = s.Size.Y/2
			local del = Vector3.new(0,seatDelt,0)+RootAttachment.Position-RootRigAttachment.Position
		
			humanoidRootPart.CFrame = s.CFrame*CFrame.new(del)

			-- 创建 WeldConstraint
			local weld = Instance.new("WeldConstraint")
			weld.Part0 = s
			weld.Part1 = humanoidRootPart
			weld.Parent = s-- 将 WeldConstraint 设置为座位的子对象
			humanoid.Sit = true


			local characterObj = InstanceManager:FindObject(character);
			self.Animator=characterObj:AddComponent(CommonOHOH.load.CharacterAnimationCtrl())
			
			self.Animator:PreLoadAnimationByRid({["drive"]="rbxassetid://127062497767575"},nil,nil,{["drive"]=true}
		)

		self.Animator:PlayAnimation("drive")
		  --"rbxassetid://127062497767575"
    		print("坐下动画已播放")
			
			print("坐车成功！")
			return true
		end
	end
	print("坐车失败！")
	return  false
end
function M.AddCharacterToDrive(self,character)
	-- print("AddCharacterToDrive")
	for _,s in pairs(self.SeatList)  do
		if  s:GetAttribute("isUsing") then
		-- 	s:Sit(character:FindFirstChildOfClass("Humanoid"))
		-- -- 	s.Disabled =true
		-- 	s:SetAttribute("isUsing",true)

		-- 	-- return true

		-- 	local humanoid = character:FindFirstChildOfClass("Humanoid")
		-- 	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")  
		-- 	local RootAttachment = humanoidRootPart:WaitForChild("RootAttachment") 
		-- 	local RootRigAttachment = humanoidRootPart:WaitForChild("RootRigAttachment") 
			
		-- 	local seatDelt = s.Size.Y/2
		-- 	local del = Vector3.new(0,seatDelt,0)+RootAttachment.Position-RootRigAttachment.Position
		
		-- 	humanoidRootPart.CFrame = s.CFrame*CFrame.new(del)

		-- 	-- 创建 WeldConstraint
		-- 	local weld = Instance.new("WeldConstraint")
		-- 	weld.Part0 = s
		-- 	weld.Part1 = humanoidRootPart
		-- 	weld.Parent = s-- 将 WeldConstraint 设置为座位的子对象
		-- 	print("坐车成功！",s)
		
        -- -- humanoid.Sit = false
        -- -- humanoid.PlatformStand = true
          -- 创建 Animation 对象并设置 AnimationId

		  
		  local characterObj = InstanceManager:FindObject(character);
			self.Animator=characterObj:AddComponent(CommonOHOH.load.CharacterAnimationCtrl())
			
			self.Animator:PreLoadAnimationByRid({["drive"]="rbxassetid://127062497767575"},nil,nil,{["drive"]=true}
		)

		self.Animator:PlayAnimation("drive")
		  --"rbxassetid://127062497767575"
    		print("坐下动画已播放")
			return true
		end
	end
	print("坐车失败！")
	return  false
end
function M.RemoveCharacterToDrive(self,character)
	print("Remove",self.SeatList)
	for _,s in pairs(self.SeatList)  do
		if  s:GetAttribute("isUsing") then
			print("isUsing",s)
			for _,child in pairs(s:GetChildren())  do	
				if child.Part1.Name== "HumanoidRootPart" then
					print("HumanoidRootPart",child)
					s.Disabled =true
					child:Destroy()
					-- s.Diabled = true
					s:SetAttribute("isUsing",false)
					if self.Animator then
						self.Animator:StopAnimation("drive")
					end
					return true
				end
			end
		end
	end
	print("离开失败！")
	return  false
end
function M.GetWeaponPostion(self)
	return self.WeaponPoint.CFrame.Position
end

function M.GetWeaponOrientation(self)
	return self.Instance.Orientation
end


return M